package com.example.afinal;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;

public class RateListActivity2 extends ListActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "RateListActivity2";
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //准备数据
        ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String,String>>();
        for (int i = 0; i < 10;i++){
            HashMap<String,String> map = new HashMap<String,String>();
            map.put("ItemTitle","Rate:  " + i);//标题文字
            map.put("ItemDetail","detail" + i);//详情描述
            listItems.add(map);
        }

        //准备适配器
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,//listItems数据源
            R.layout.list_item,//ListItem的XML 布局实现
            new String[] { "ItemTitle" , "ItemDetail" },
            new int[] { R.id.itemTitle, R.id.itemDetail }
        );

        //绑定
        setListAdapter(listItemAdapter);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg){
                if (msg.what == 11){
                    Toast.makeText(RateListActivity2.this,"数据已更新",Toast.LENGTH_SHORT).show();
                    ArrayList<RateItem> list2 = (ArrayList<RateItem>) msg.obj;
//                    SimpleAdapter listItemAdapter = new SimpleAdapter(RateListActivity2.this,list2,
//                            R.layout.list_item,
//                            new String[] { "Title" , "detail" },
//                            new int[] { R.id.itemTitle, R.id.itemDetail }
//                    );
                    MyAdapter myAdapter = new MyAdapter(RateListActivity2.this,R.layout.list_item,list2);
                    setListAdapter(myAdapter);
                }
                super.handleMessage(msg);
            }
        };

        //开启线程
        MyListThread2 mythread = new MyListThread2();
        mythread.setHandler(handler);
        Thread t = new Thread(mythread);
        t.start();

        //绑定事件处理
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String,String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
        Log.i(TAG,"onItemClick: ");
        String name = map.get("Title");
        String rate = map.get("detail");
        Log.i(TAG,"onItemClick: name=" + name + "==>" + rate);
    }
}